package com.achievo.know.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.util.NodeList;
import org.htmlparser.visitors.HtmlPage;

import com.achievo.know.pojo.Constants;
import com.dayatang.domain.BaseEntity;
import com.dayatang.domain.InstanceFactory;


@Entity
@Table(name="RAMS_NEWS_CONTENT")
public class NewsContent extends BaseEntity{

	/**
	 * 
	 */
	private static final long serialVersionUID = 5355569824684959472L;
	//内容
	@Column(name="CONTENT")
	@Lob
	private String content;
	
	
	@ManyToOne(cascade=CascadeType.REFRESH,optional=false)  
	@JoinColumn(name="FK_NEWS_ID")
	private News news;
	
	@Transient
	private String[] contentPage;
	
	@Transient
	public int getWordCount(String s)
    {

        s = s.replaceAll("[^\\x00-\\xff]", "**");
        s = s.replaceAll("&nbsp;", "*");
        int length = s.length();
        return length;
    }
	
	public String[] getContentPage() {
		if(StringUtils.isNotEmpty(content)){
			try {
				List<String> cp=new ArrayList<String>();
				contentPage=content.split(Constants.PAGELINE);
				Parser p=Parser.createParser("<html><body>"+content+"</body></html>", "GBK");
				HtmlPage visitor=new HtmlPage(p);
				p.visitAllNodesWith(visitor);
				NodeList list=visitor.getBody();
				Node[] ns=list.toNodeArray();
				StringBuilder sb=new StringBuilder();
				StringBuilder sbHtml=new StringBuilder();
				int i=0;
				for(Node n:ns){
					sb.append(n.toPlainTextString());
					sbHtml.append(n.toHtml());
					int length=getWordCount(sb.toString());
					if(i++>100 || length>4000){
						cp.add(sbHtml.toString());
						sb.delete(0, sb.length());
						sbHtml.delete(0, sbHtml.length());
						i=0;
					}
				}
				if(StringUtils.isNotBlank(sb.toString()))
					cp.add(sbHtml.toString());
				contentPage=cp.toArray(contentPage);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		return contentPage;
	}

	public void setContentPage(String[] contentPage) {
		this.contentPage = contentPage;
	}

	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@Override
	public boolean equals(Object arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	private static NewsContentRepository newsContentRepository;
	
	public static NewsContentRepository newsContentRepository() {
		if (newsContentRepository == null) {
			newsContentRepository = InstanceFactory.getInstance(NewsContentRepository.class);
		}
		return newsContentRepository;
	}

	public void add() {
		newsContentRepository().save(this);
		
	}
	
	
	
}
